//
//  BKShangShiViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/2.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKShangShiViewController.h"
#import "BKSearchField.h"
#import "BKShangShiViewModel.h"
#import "BKResultOfShangShiViewController.h"
#import <MBProgressHUD.h>
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "BKHistoryViewModel.h"
#import "BKSearchPickViewDataSource.h"

@interface BKShangShiViewController ()
@property (weak, nonatomic) IBOutlet BKSearchField *fenlei;
@property (weak, nonatomic) IBOutlet BKSearchField *zhiwei;
@property (weak, nonatomic) IBOutlet BKSearchField *pici;
@property (weak, nonatomic) IBOutlet BKSearchField *suozaidi;
@property (weak, nonatomic) IBOutlet BKSearchField *xuexiao;
@property (weak, nonatomic) IBOutlet BKSearchField *xueli;
@property (copy, nonatomic) NSArray *colleageArray;
@end

@implementation BKShangShiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //_xuexiao.flag = 8004;
    
    _xueli.text = @"大专";
    _xueli.dataArray = @[@"大专",@"本科",@"硕士",@"博士"];
    _xueli.flag = 8011;
    
    _zhiwei.text = @"不限";
    _zhiwei.flag = 8008;
    
    _fenlei.text = @"不限";
    _fenlei.flag = 8007;
    
    _pici.dataArray = @[@"提前批",
                        @"本科一批",
                        @"本科二批",
                        @"本科三批",
                        @"高职专科"];
    _pici.flag = 8011;
    
    @weakify(self);
    [RACObserve(self.suozaidi, text) subscribeNext:^(id x) {
        @strongify(self);
        
        [self getColleageDataSourceArray];
    }];
    [RACObserve(self.pici, text) subscribeNext:^(id x) {
        @strongify(self);
        
        [self getColleageDataSourceArray];
        
    }];

    
}

- (void)getColleageDataSourceArray
{
    if (self.pici.text.length > 0 && self.suozaidi.text.length > 0) {
        self.xuexiao.text = @"";
        self.xuexiao.dataArray = @[];
        
        [BKShangShiViewModel getColleageWithSuozaidi:self.suozaidi.text pici:self.pici.text success:^(NSArray *dataArray) {
            NSMutableArray *showArray = [NSMutableArray array];
            for (NSDictionary *dict in dataArray) {
                [showArray addObject:dict[@"name"]];
            }
            self.colleageArray = dataArray;
            self.xuexiao.dataArray = showArray;
            self.xuexiao.flag = 8011;
            
        } failure:^(NSString *message) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [alert show];
            
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)p_submit:(id)sender {
    
    if ([_suozaidi.text length] == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择校友所在地" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    if ([_pici.text length] == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择批次" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    if ([_xuexiao.text length] == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择学校" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    
    __block NSString *cid = @"";
    __block NSString *feileiId = @"";
    __block NSString *zhiweiId = @"";
    __block NSString *xueliId = @"";
    
    [self.colleageArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[NSDictionary class]]) {
            if ([obj[@"name"] isEqualToString:_xuexiao.text] ) {
                *stop = YES;
                cid = obj[@"cid"]?:@"";
            }
        }
    }];
    
    [_fenlei.pickViewDataSource.feileiArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isEqualToString:_fenlei.text] ) {
            *stop = YES;
            feileiId = [NSString stringWithFormat:@"%ld",idx];
        }
    }];
    
    [_zhiwei.pickViewDataSource.zhiweiArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isEqualToString:_zhiwei.text] ) {
            *stop = YES;
            zhiweiId = [NSString stringWithFormat:@"%ld",idx];
        }
    }];
    
    [_xueli.dataArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isEqualToString:_xueli.text] ) {
            *stop = YES;
            xueliId = [NSString stringWithFormat:@"%ld",idx + 1];
        }
    }];
    

    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"正在加载";
    [self.view addSubview:hud];
    [hud show:YES];
    
    UIButton *button = (UIButton *)sender;
    button.enabled = NO;
    
    [BKShangShiViewModel shangshiWithColleageCid:cid fenlei:feileiId zhiwei:zhiweiId xueli:xueliId page:nil len:nil success:^(NSArray *dataArray) {
        [hud hide:YES];
        [hud removeFromSuperview];
        button.enabled = YES;
        BKResultOfShangShiViewController *shangshi = [[BKResultOfShangShiViewController alloc] initWithNibName:NSStringFromClass([BKResultOfShangShiViewController class]) bundle:nil];
        shangshi.dataArray = dataArray;
        NSLog(@"%@",dataArray);
        [self.navigationController pushViewController:shangshi animated:YES];
    } failure:^(NSString *message) {
        [hud hide:YES];
        [hud removeFromSuperview];
        button.enabled = YES;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alert show];
        
    }];
//
//    [BKShangShiViewModel shangshiWithParams:params success:^(NSDictionary *dataDic) {
//        [hud hide:YES];
//        BKResultOfShangShiViewController *shangshi = [[BKResultOfShangShiViewController alloc] initWithNibName:NSStringFromClass([BKResultOfShangShiViewController class]) bundle:nil];
//        shangshi.dataDic = dataDic;
//        [self.navigationController pushViewController:shangshi animated:YES];
//    } failure:^(NSString *message) {
//               dispatch_async(dispatch_get_main_queue(), ^{
//                   [hud hide:YES];
//                   
//
//            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
//            [alertView show];
//        });
//
//    }];
    
    
}
- (IBAction)p_dismiss:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
